#* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#*   gbx-emu - SConstruct                                                  *
#*   gbx-emu homepage: http://code.google.com/p/gbx-emu/                   *
#*   Copyright (C) 2009 olejl77@gmail.com                                  *
#*                                                                         *
#*   This program is free software: you can redistribute it and/or modify  *
#*   it under the terms of the GNU General Public License as published by  *
#*   the Free Software Foundation, either version 3 of the License, or     *
#*   (at your option) any later version.                                   *
#*                                                                         *
#*   This program is distributed in the hope that it will be useful,       *
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          *
#*   GNU General Public License for more details.                          *
#*                                                                         *
#*   You should have received a copy of the GNU General Public License     *
#*   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
#* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

import os
import platform
import sys

#From SCons Wiki
def print_cmd_line(s, target, src, env):
    """s is the original command line, target and src are lists of target
    and source nodes respectively, and env is the environment."""
    sys.stdout.write(" Making %s...\n"%\
         (' and '.join([str(x) for x in target])))

vars = Variables('custom.py')
vars.Add(BoolVariable('verbose','Print more information during build', 0))
vars.Add(BoolVariable('debug','Debug build', 0))
vars.Add(EnumVariable('build', 'Choose a build flavor', 'release', allowed_values = ('release', 'debug'), ignorecase = 2))
unknown = vars.UnknownVariables()
if unknown:
    print "Unknown variables:", unknown.keys()
    Exit(1)
env = Environment(variables = vars)
vars.Save('custom.py', env)

# Compiler Warnings
env.AppendUnique(CCFLAGS = '-ansi')
env.AppendUnique(CCFLAGS = '-Wall')
env.AppendUnique(CCFLAGS = '-W')
env.AppendUnique(CCFLAGS = '-Wcast-qual')
env.AppendUnique(CCFLAGS = '-Wwrite-strings')
#env.AppendUnique(CCFLAGS = '-pedantic')
#env.AppendUnique(CCFLAGS = '-Wconversion')
#env.AppendUnique(CCFLAGS = '-Wshadow')

# Build type specifics
if env['build'] == 'debug':
    env.AppendUnique(CCFLAGS = '-g')
    env.AppendUnique(CCFLAGS = '-D_DEBUG_')
else:
    env.AppendUnique(CCFLAGS = '-O2')
if not env['verbose']:
    env['PRINT_CMD_LINE_FUNC'] = print_cmd_line

# Platform spesific
if sys.platform == 'win32':
    env.AppendUnique(CCFLAGS = '-DWIN32')
    print 'win32'
elif sys.platform == 'darwin':
    env.AppendUnique(CCFLAGS = '-DDARWIN')
    print 'darwin'
elif sys.platform == 'linux2':
    env.AppendUnique(CCFLAGS = '-DLINUX')

conf = Configure(env)

qtdir = os.popen('qmake -query QT_INSTALL_PREFIX').read().strip()
if qtdir.find('qt-3') >= 0:     # On some systems qmake-q4 has to be used
    qtdir = os.popen('qmake-qt4 -query QT_INSTALL_PREFIX').read().strip()
if not qtdir:   
    Exit(1)

env.Append(QTDIR = qtdir)
env.Tool('qt4')
env.EnableQt4Modules(['QtCore', 'QtGui'])
env = conf.Finish()

svn_revision = os.popen('svn info | grep "Revision: "').read().strip()
svn_url = os.popen('svn info | grep "URL: "').read().strip()

# Update the version.h file
infile = open('version.template')
outfile = open('version.h','w')
for line in infile:
    if 'GBX_EMU_VERSION' in line:
        line = "#define GBX_EMU_VERSION \"" + svn_revision + "\"\n"
    outfile.write(line)
infile.close()
outfile.close() 

src = [
    'gui/aboutdialog.cpp',
    'gui/breakpointswidget.cpp',
    'core/cpu.cpp',
    'core/debugger.cpp',
    'gui/debuggerwidget.cpp',
    'gui/disasmmodel.cpp',
    'core/gameboy.cpp',
    'gui/getinput.cpp',
    'core/main.cpp',
    'gui/mainwindow.cpp',
    'core/memory.cpp',
    'gui/registermodel.cpp',
    'gui/settingsdialog.cpp',
    'core/screen.cpp',
    ]

ui = [
    'gui/aboutdialog.ui',
    'gui/breakpointswidget.ui',
    'gui/debuggerwidget.ui',
    'gui/getinput.ui',
    'gui/mainwindow.ui',
    'gui/settingsdialog.ui',
    ]

env.Uic4(ui)
env.Program(target = ['gbx-emu'], source = [src])

Help(vars.GenerateHelpText(env))
